﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class CompanyContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取客户联系人信息
        /// </summary>
        public void GetContactList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_CompanyContact, bool>> exp = SearchExpression.FindByGroup<BD_CompanyContact>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_CompanyContactDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.BD_CompanyContactDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 获取客户的所有联系人
        /// </summary>
        public void GetAllContactNameList() {
            int Id = Commons.String2Int32(Request["companyID"]);
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_CompanyContactDAL.GetListBy(cc=>cc.CompanyID==Id).Select(p => new { value = p.ID, text = p.Person })));      
        }

        /// <summary>
        /// 根据q获取客户联系人信息
        /// </summary>
        public void GetContactInfoByQ()
        {
            string q = Request.Form["q"];
            var a = new List<BD_CompanyContact>();
            if (q == null)
            {
                var list = DALSession.Current.BD_CompanyContactDAL.GetListBy(ct => ct.ID!=null);
                Response.Write(JsonHelper.toPageJson(list, q.Count()));
            }
            else
            {
                var list = DALSession.Current.BD_CompanyContactDAL.GetListBy(ct=>ct.Person.Contains(q));
                Response.Write(JsonHelper.toPageJson(list, q.Count()));
            }
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        public void EditContactInfo()
        {
            String msg= "" ;
            Boolean res = false;
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_CompanyContact perNew = DALSession.Current.BD_CompanyContactDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            if (checkContact(perNew, msg)) {
                DALSession.Current.BD_CompanyContactDAL.ModifyAllModel(perNew);
                DALSession.Current.BD_CompanyContactDAL.SaveChanges();
                res = true;
                msg = "保存成功！";
            }

            Response.Write(JsonHelper.toDefaultMsg(res, msg));
        }
        /// <summary>
        /// 添加客户信息
        /// </summary>
        public void AddContactInfo()
        {
            String msg="";
            Boolean res = false;
            BD_CompanyContact per = new BD_CompanyContact();
            ModelTools.Form2EditModel(Request.Form, per);
            if (checkContact(per, msg)) {
                DALSession.Current.BD_CompanyContactDAL.Add(per);
                DALSession.Current.BD_CompanyContactDAL.SaveChanges();
                res = true;
                msg = "保存成功！";
            }
            Response.Write(JsonHelper.toDefaultMsg(res, msg));
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        public void DelContactInfo()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_CompanyContactDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_CompanyContactDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        public Boolean checkContact(BD_CompanyContact q, String msg)
        {
            Boolean res = true;
            if (String.IsNullOrEmpty(q.CompanyID.ToString()))
            {
                msg = "客户ID不能为空！";
                res = false;
            }
            return res;
        }

    }
}